﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RBAC.Web.Service;
using RBAC.Web.Models;
namespace RBAC.Web.Mapper
{
    public class PermissionMapper
    {
        public static PermissionModel DtoToModel(PermissionDto inputDto)
        {
            PermissionModel result = new PermissionModel();

            result.Id = inputDto.Id;
            result.PermissionName = inputDto.PermissionName;
            result.Description = inputDto.Description;
            //result.CreateDate = inputDto.CreateDate;
            result.CreateDate = inputDto.CreateDate;
            result.CreateById = inputDto.CreateById;
            result.PermissionStatus = inputDto.IsActive.GetValueOrDefault() ? 1 : 2;

            if (inputDto.Role != null)
            {
                result.Role = inputDto.Role.Id;

                result.RoleId = inputDto.Role.Id;
                result.RoleRoleCode = inputDto.Role.RoleCode;

            }

            if (inputDto.Function != null)
            {
                result.Function = inputDto.Function.Id;

                result.FunctionId = inputDto.Function.Id;
                result.FunctionFuncName = inputDto.Function.FuncName;

            }
            //result.PermissionStatus = inputDto.PermissionStatus;

            if (inputDto.RoleLookup != null)
            {
                result.RoleLookup = inputDto.RoleLookup.Id;

                result.RoleLookupId = inputDto.RoleLookup.Id;
                result.RoleLookupRoleCode = inputDto.RoleLookup.RoleCode;

            }

            return result;
        }


        public static PermissionDto ModelToDto(PermissionModel inputModel)
        {


            PermissionDto result = new PermissionDto();

            result.Id = inputModel.Id;
            result.PermissionName = inputModel.PermissionName;
            result.Description = inputModel.Description;
            //result.CreateDate = inputModel.CreateDate;
            if (inputModel.Role != 0)
            {
                result.Role = new RoleDto() { Id = inputModel.Role };
            }
            else
            {
                //result.Role = new RoleDto();
                result.Role = null;
            }

            if (inputModel.Function != 0)
            {
                result.Function = new FunctionDto() { Id = inputModel.Function };
            }
            else
            {
                //result.Function = new FunctionDto();
                result.Function = null;
            }

            //result.PermissionStatus = inputModel.PermissionStatus;
            if (inputModel.RoleLookup != 0)
            {
                result.RoleLookup = new RoleDto() { Id = inputModel.RoleLookup };
            }
            else
            {
                //result.RoleLookup = new RoleDto();
                result.RoleLookup = null;
            }

            result.CreateDate = inputModel.CreateDate;
            result.CreateById = inputModel.CreateById ;
            result.IsActive = inputModel.PermissionStatus == 1;


            return result;
        }

    }
}